package com.doit.day05

object _02_算子 {
  def main(args: Array[String]): Unit = {
    //折叠算子
    val arr: Array[Int] = Array(1, 2, 3, 4, 5)

    //collect  ==》 小需求：我想要里面所有的奇数，并且将每个奇数都乘以10   filter和map的结合
    println(arr.filter(_ % 2 == 1).map(_ * 10+"").toList)

    println(arr.collect(new PartialFunction[Int, String] {
      //就类似于filter
      override def isDefinedAt(x: Int) = x % 2 == 1

      //类似于map
      override def apply(v1: Int) = v1 * 10 + ""
    }).toList)


    //模式匹配
    val array: Array[Any] = Array(1, "bao", 1.0, true, 'a', (1, 1.0), 10)
    //将所有的int类型都取出来
    println(array.collect(new PartialFunction[Any, Int] {
      override def isDefinedAt(x: Any) = x.isInstanceOf[Int]

      override def apply(v1: Any) = v1.asInstanceOf[Int]
    }).toList)


    /*    val res: Int = arr.reduce(_+_)
        println(res)

        val res2: Int = arr.reduce(_-_)//1-2 =-1 -4 -8 -13
        println(res2)//-13

        println(arr.reduceRight(_ + _))//15

        println(arr.reduceRight(_ - _))//3   -2*/
    val ints1: Array[Int] = arr.take(2)
    println(ints1.toList)//List(1, 2)
    println(arr.takeRight(2).toList)
    println(arr.takeWhile(_ % 2 == 1).toList)//List(1)


    println("=========保迟到的分隔符==========")

    println(arr.fold(10)(_ + _)) //25

    //单机的
    println(arr.foldRight(10)(_ - _)) //-7


    val ints: Array[Int] = Array(1, 2, 3, 1, 2, 3, 12, 4)

    val distinct: Array[Int] = ints.distinct
    println(distinct.mkString(","))

    val baos: Array[Bao] = Array(
      Bao(1, "bao", 18),
      Bao(2, "bao", 19),
      Bao(2, "xu", 18)
    )

    //minBy maxBy
    println(baos.maxBy(_.name))

    //sorted  sortWith   sortBy
    //    println(baos.sorted)

//        baos.sortBy(bao=>{
//          (-bao.id,bao.name)
//        })

    //id的降序，如果相同，比较name的升序
    println(baos.sortWith((b1, b2) => {
      if (b1.id == b2.id) {
        val i: Int = b2.name.compareTo(b1.name)
        if (i >= 0) true else false
      } else {
        true
      }
    }).toList)

    //想要比较  sorted 只能比较可比较的东西   sortBy 比较可比较的属性，一般情况下都是比较一个属性的类型是int  double  String (元祖)
    //sortWith 是直接传一个比较规则进去




  }
}


case class Bao(id: Int, name: String, age: Int)

/*extends Ordered[Bao] {
 override def compare(that: Bao): Int = this.id - that.id
}*/
